#include <functional>
#include <memory>
#include <string>

#include "AsyncWorker.hpp"
#include "Client_Connection.hpp"
#include "Message.pb.h"
#include "client_channel.hpp"
#include "fmt/core.h"
class Init
{
    public:
        Init()
        {
            fmtlog::setLogLevel(fmtlog::DBG);
            fmtlog::setHeaderPattern("{s} \t| {t} \t| {l} \t| {YmdHMS} \t| {g} \t| \n\t:");
            fmtlog::startPollingThread(12);
        }
};
void cb(MY_MQ::ClientChannel::self_ptr& channel, const std::string& consumerID, const MQ_Protobuf::BasicProperties* bp,
        const std::string& body) {
    std::cout << "consumerID:" << consumerID << " body:" << body << std::endl;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        fmt::print("usage: ./consume_client queue1\n");
    }
    Init _____DONT_USE_ME__ONLY_INIT_LOG_____;
    MY_MQ::AsyncWorker::self_ptr       AsWorker = std::make_shared<MY_MQ::AsyncWorker>();
    MY_MQ::Client_Connection::self_ptr conn = std::make_shared<MY_MQ::Client_Connection>("127.0.0.1", 8888, AsWorker);
    MY_MQ::ClientChannel::self_ptr     channel = conn->openChannel();
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->declareExchange("exchange1", MQ_Protobuf::ExchangeType::TOPIC, 0, 0, tmp_map);
    channel->DeclareQueue("queue1", 1, 0, 0, tmp_map);
    channel->DeclareQueue("queue2",1,0,0,tmp_map);
    channel->QueueBind("exchange1", "queue1", "queue1");
    channel->QueueBind("exchange1", "queue2", "news.music.#");
    auto func = std::bind(cb,channel,std::placeholders::_1,std::placeholders::_2,std::placeholders::_3);
    std::cout<<"开始消费消息\n";
    channel->basicConsume("consumer1", argv[1], 0, func);
    while(1)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }
    conn->closeChannel(channel);
    return 0;
}